package com.wulian.common.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wulian.common.entity.SysUser;
import com.wulian.common.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;

public class ListUtil {
	/**
	* 将JavaBean转换成Map
	*
	* @param obj
	* @return
	* @throws SecurityException
	* @throws NoSuchMethodException
	* @throws InvocationTargetException
	* @throws IllegalArgumentException
	* @throws IllegalAccessException
	*/
	public static Map beanToMap(Object obj,String includeFields) throws NoSuchMethodException, SecurityException, IllegalAccessException,
	IllegalArgumentException, InvocationTargetException {
	// 创建map集合
	Map map = new HashMap();
	// 获取JavaBean中所有属性
	Field[] fields = obj.getClass().getDeclaredFields();
	for (Field fie : fields) {
		if (Modifier.isFinal(fie.getModifiers())) continue;
	// 将属性第一个字母转换成大写
	String frist = fie.getName().substring(0, 1).toUpperCase();
	// 获取属性的类型
	Class<?> type = fie.getType();
	// 封装属性的get
	String getter = "";
	if ("boolean".equals(type.getName())) {
	getter = "is" + frist + fie.getName().substring(1);
	} else {
	getter = "get" + frist + fie.getName().substring(1);
	}
	// 获取JavaBean的方法
	Method method = obj.getClass().getMethod(getter, new Class[] {});
	// 调用方法,并接收返回值
	Object objec = method.invoke(obj, new Object[] {});
	
	//增加筛选字段
	if(StringUtils.isBlank(includeFields)){
		// 判断返回值不为空
		if (objec != null) {
		map.put(fie.getName(), objec);
		} else {
		map.put(fie.getName(), "");
		}
		}else{
			if(includeFields.contains(fie.getName())){
				// 判断返回值不为空
				if (objec != null) {
				map.put(fie.getName(), objec);
				} else {
				map.put(fie.getName(), "");
				}
			}
		}
	}

	return map;
	}
	/**
	* 将Map转换为JavaBean
	*
	* @param map
	* @param obj
	* @return
	* @throws SecurityException
	* @throws NoSuchMethodException
	* @throws InvocationTargetException
	* @throws IllegalArgumentException
	* @throws IllegalAccessException
	*/
	public static Object mapToBean(Map<String, Object> map, Object obj) throws NoSuchMethodException, SecurityException,
	IllegalAccessException, IllegalArgumentException, InvocationTargetException {
	// 获取JavaBean中的所有属性
	Field[] field = obj.getClass().getDeclaredFields();
	for (Field fi : field) {
	// 判断key值是否存在
	if (map.containsKey(fi.getName())) {
	// 获取key的value值
	String value = map.get(fi.getName()).toString();
	// 将属性的第一个字母转换为大写
	String frist = fi.getName().substring(0, 1).toUpperCase();
	// 属性封装set方法
	String setter = "set" + frist + fi.getName().substring(1);
	// 获取当前属性类型
	Class<?> type = fi.getType();
	// 获取JavaBean的方法,并设置类型
	Method method = obj.getClass().getMethod(setter, type);
	// 判断属性为double类型
	if ("java.lang.String".equals(type.getName())) {
	// 调用当前Javabean中set方法，并传入指定类型参数
	method.invoke(obj, value);
	} else if ("int".equals(type.getName())) {
	method.invoke(obj, Integer.parseInt(value));
	}else if ("double".equals(type.getName())) {
	method.invoke(obj, Double.valueOf(value));
	} else if ("char".equals(type.getName())) {
	method.invoke(obj, value);
	}
	}
	}
	return obj;
	}
	/**
	* 将List<Map<String,Object>>转换成List<javaBean>
	*
	* @param listm
	* @param obj
	* @return
	* @throws InvocationTargetException
	* @throws IllegalArgumentException
	* @throws IllegalAccessException
	* @throws SecurityException
	* @throws NoSuchMethodException
	*/
	public static Object ListMapToListBean(List<Map<String, Object>> listm, Object obj)
	throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
	InvocationTargetException {
	List<Object> list = new ArrayList<Object>();
	// 循环遍历出map对象
	for (Map<String, Object> m : listm) {
	// 调用将map转换为JavaBean的方法
	Object objs = mapToBean(m, obj);
	// 添加进list集合
	list.add(objs);
	}
	return list;
	}
	/**
	* 将list<javabean>转换为List<Map>
	*
	* @param list
	* @return
	* @throws NoSuchMethodException
	* @throws SecurityException
	* @throws IllegalAccessException
	* @throws IllegalArgumentException
	* @throws InvocationTargetException
	*/
	public static List<Map<String, Object>> ListBeanToListMap(List<?> list,String includeFields) throws NoSuchMethodException,
	SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
	List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
	for (Object ob : list) {
	listmap.add(beanToMap(ob,includeFields));
	}
	return listmap;
	}
	
	public static Map beanToNullValMap(Object obj,String includeFields) throws NoSuchMethodException, SecurityException, IllegalAccessException,
	IllegalArgumentException, InvocationTargetException {
	// 创建map集合
	Map map = new HashMap();
	// 获取JavaBean中所有属性
	Field[] fields = obj.getClass().getDeclaredFields();
	for (Field fie : fields) {
		if (Modifier.isFinal(fie.getModifiers())) continue;
	
	//增加筛选字段
	if(StringUtils.isBlank(includeFields)){
		map.put(fie.getName(), "");
		}else{
			if(includeFields.contains(fie.getName())){
				map.put(fie.getName(), "");
			}
		}
	}

	return map;
	}
	
	public static List<Map<String, Object>> ListBeanToListNullValMap(List<?> list,String includeFields) throws NoSuchMethodException,
	SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
	List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
	for (Object ob : list) {
	listmap.add(beanToNullValMap(ob,includeFields));
	}
	return listmap;
	}

	public static <T> List<T>  copyListBean(List<?> reource , T targe){
		List<T> reslt = new ArrayList<>();
		for(int i = 0 ; i < reource.size() ;i++){
			Object o = reource.get(i);
			T t = (T)o;
			BeanUtils.copyProperties(o,t);
			reslt.add(t);
		}
		return reslt;
	}


}
